import utils.TreeNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/16 14:07
 * @description:
 */
public class Solution111 {
    int currentDepth = 0;
    int res =Integer.MAX_VALUE;
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        } else if (root.left == null && root.right == null) {
            return 1;
        } else {
            int left = Integer.MAX_VALUE;
            if (root.left != null) {
                left = minDepth(root.left);
            }
            int right = Integer.MAX_VALUE;
            if (root.right != null) {
                right = minDepth(root.right);
            }
            return Math.min(left, right) + 1;

        }
    }
    public void traverse(TreeNode root){
        if(root == null){
            return;
        }
        currentDepth++;
        traverse(root.left);
        traverse(root.right);
        if(root.left == null && root.right ==null){
            res = Math.min(res,currentDepth);
        }
        currentDepth--;

    }

    public static void main(String[] args) {
        Solution111 solution = new Solution111();
        TreeNode root = TreeNode.buildTree(new int[]{2, -1, 3, -1, 4, -1, 5, -1, 6});
        TreeNode.show(root);
        System.out.println(solution.minDepth(root));
    }
}
